package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.bean.Category;
import com.itheima.bean.PageParam;
import com.itheima.dao.DishDao;
import com.itheima.dto.DishDto;
import com.itheima.service.CategoryService;
import com.itheima.service.DishFlavorService;
import com.itheima.service.DishService;
import com.itheima.bean.Dish;
import com.itheima.bean.DishFlavor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Transactional
@Service
public class DishServiceImpl extends ServiceImpl<DishDao, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private CategoryService categoryService;
    //根据分类的Id查询菜品数据
    @Override
    public List<DishDto> findByCid(long cid, Integer status) {
        //1.查询菜品数据
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();

        lqw.eq(Dish::getCategoryId,cid);
        lqw.eq(status != null, Dish::getStatus,status);

        List<Dish> dishList = list(lqw);

        //2.查询口味数据
      return  dishList.stream().map(dish -> {
            //2.1构建DishDto对象
            DishDto dto = new DishDto();

            //2.2把dto数据拷贝到DishDto中去,一个dish映射成一个半dto
            BeanUtils.copyProperties(dish,dto);

            //2.3封装口味数据
            //2.3.1根据菜品的id去查询口味数据
            dto.setFlavors(dishFlavorService.findByDishId(dish.getId()));

            return dto;
        }).collect(Collectors.toList());

    }

    //添加菜品和口味
    @Override
    public boolean add(DishDto dishDto) {
        //1.先往菜品表添加数据
        boolean result1 = save(dishDto);

        //2.再往口味表里添加数据
        List<DishFlavor> flavorList = dishDto.getFlavors();

        boolean result2=true;
        //如果有口味数据才进行操作
        if(flavorList.size()>0){

            //给每一个对象都添加菜品的ID
            result2=dishFlavorService.saveBatch(flavorList.stream().map(flavor ->{
                flavor.setDishId(dishDto.getId());
                return flavor;
            }).collect(Collectors.toList()));

        }
        return result1 && result2 ;
    }

    //菜品分页
    @Override
    public IPage<DishDto> page(PageParam pageParam) {

        //1.构建分页对象
        IPage<Dish> page = new Page<>(pageParam.getPage(), pageParam.getPageSize());

        //2.构建条件对象
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();

        //3.设置条件
        lqw.like(pageParam.getName()!=null, Dish::getName,pageParam.getName());

        //4.执行分页查询
        page(page,lqw);

        //5.构建一个全新的Page对象,里面装DishDto
        Page<DishDto> dtoPage = new Page<>();

        //5.1需要设置total数据: 用来标识总数
        dtoPage.setTotal(page.getTotal());
        //5.2设置records数据 用来表示总数

        //5.2.1 List<>
        List<DishDto> dtoList = page.getRecords().stream().map(dish -> {
            //a.构建DishDto
            DishDto dishDto = new DishDto();

            //b.把dish的数据拷贝到dishDto来
            BeanUtils.copyProperties(dish,dishDto);

            //c.把分类的名字储存到dishDto
            Category category = categoryService.getById(dish.getCategoryId());
            dishDto.setCategoryName(category.getName());
            return dishDto;
        }).collect(Collectors.toList());

        dtoPage.setRecords(dtoList);

        return dtoPage;
    }

    //根据Id查询菜品信息和口味信息
    @Override
    public DishDto findById(Long id) {
        //1. 构建一个DishDto
        DishDto dto  = new DishDto();

        //2. 查询菜品数据
        // 使用父亲的写法
        Dish dish = getById(id);

        //使用dao的写法
        //getBaseMapper().selectById(id);

        //3. 查询口味数据
        List<DishFlavor> flavorList = dishFlavorService.findByDishId(id);

        //4. 组装数据
        BeanUtils.copyProperties(dish , dto);
        dto.setFlavors(flavorList);
        return dto;
    }

    //根据Id修改并更新菜品信息
    @Override
    public boolean update(DishDto dishDto) {
        //1.先更新菜品表
        boolean result1 = updateById(dishDto);

        //2.再更新口味表
        //2.1先删除口味数据
        dishFlavorService.deleteByDishId(dishDto.getId());

        //2.2再添加口味数据
        List<DishFlavor> flavorList = dishDto.getFlavors();

        boolean result2=true;


        //如果页面有传过来的口味数据,才执行操作
        if(flavorList.size()>0){

            List<DishFlavor> list = flavorList.stream().map(dishFlavor -> {

                //给每一个菜品都添加ID
                dishFlavor.setDishId(dishDto.getId());
                return dishFlavor;
            }).collect(Collectors.toList());

            //批量添加
             result2=dishFlavorService.saveBatch(list);
        }

        return result1 && result2;
    }

    //
}
